package routes

import (
	"github.com/gin-gonic/gin"
	"go-admin-system/controllers"
	"go-admin-system/middleware"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

func SetupRoutes(r *gin.Engine, db *gorm.DB, logger *zap.Logger) {
	// 创建控制器实例
	userController := controllers.NewUserController(db)
	authController := controllers.NewAuthController(db, logger)
	roleController := controllers.NewRoleController(db)
	permissionController := controllers.NewPermissionController(db)
	menuController := controllers.NewMenuController(db)

	// 公共路由组
	public := r.Group("/api")

	// 认证相关路由
	public.POST("/login", authController.Login)

	// 受保护的路由组
	protected := r.Group("/api")
	// 使用全局认证中间件变量而不是直接调用函数
	protected.Use(middleware.GlobalAuthMiddleware)

	// 用户管理路由
	protected.GET("/users", userController.GetUsers)
	protected.GET("/users/:id", userController.GetUser)
	protected.POST("/users", userController.CreateUser)
	protected.PUT("/users/:id", userController.UpdateUser)
	protected.DELETE("/users/:id", userController.DeleteUser)

	// 角色管理路由
	protected.GET("/roles", roleController.GetRoles)
	protected.GET("/roles/:id", roleController.GetRole)
	protected.POST("/roles", roleController.CreateRole)
	protected.PUT("/roles/:id", roleController.UpdateRole)
	protected.DELETE("/roles/:id", roleController.DeleteRole)
	protected.POST("/roles/:id/permissions", roleController.AssignPermissions)

	// 权限管理路由
	protected.GET("/permissions", permissionController.GetPermissions)
	protected.GET("/permissions/:id", permissionController.GetPermission)
	protected.POST("/permissions", permissionController.CreatePermission)
	protected.PUT("/permissions/:id", permissionController.UpdatePermission)
	protected.DELETE("/permissions/:id", permissionController.DeletePermission)

	// 菜单管理路由
	protected.GET("/menus", menuController.GetMenus)
	protected.GET("/menus/:id", menuController.GetMenu)
	protected.POST("/menus", menuController.CreateMenu)
	protected.PUT("/menus/:id", menuController.UpdateMenu)
	protected.DELETE("/menus/:id", menuController.DeleteMenu)
	protected.POST("/menus/:id/roles", menuController.AssignRoles)
}
